package fr.pud.server.database;

import java.util.ArrayList;

/**
 * @author Adrien Hierry
 */
public class Way {
    // Attributes
    /**
     * an ArrayList<long> characterizing the nodes of this Way
     */
    private ArrayList<Long> nodes;
    /**
     * an int giving access to the actual node of this Way
     */
    private int             cursor;

    // constructors
    /**
     * constructor with no parameters
     */
    public Way() {
        this.nodes = new ArrayList<Long>();
        this.cursor = 0;
    }

    /**
     * constructor with a nodes ArrayList and a new cursor
     * @param n
     * @param c
     */
    public Way(ArrayList<Long> n, int c) {
        this.nodes = n;
        this.cursor = c;
    }

    /**
     * constructor using a String which got the format
     * "{long1,long2,...,longn,long1}"
     * which takes each value of long and includes in the attribute nodes
     * @param s
     */
    public Way(String s) {
        this.nodes = new ArrayList<Long>();
        this.cursor = 0;
        int i = 0;
        s = s.substring(1);
        // System.out.println(s);
        while (s.charAt(0) != '}') {
            // System.out.println(s.charAt(i));
            if (i != 0) {
                s = s.substring(1);
            }
            // System.out.println(tour);
            // System.out.println("tata");
            long j;
            // System.out.println(s.indexOf(","));
            String parser = "";
            if (s.indexOf(",") != -1) {
                parser = ",";
            }
            else {
                parser = "}";
            }
            j = Long.parseLong(s.substring(0, s.indexOf(parser)));
            // System.out.println(s);
            this.nodes.add(j);
            s = s.substring(s.indexOf(parser));
            // System.out.println(s);
            i++;
        }
    }

    /**
     * Return the actual node specified by the attribute cursor
     * @return long
     */
    public long getAct() {
        return this.getNode(this.getCursor());
    }

    /**
     * Return the attribute cursor
     * @return int
     */
    public int getCursor() {
        return this.cursor;
    }

    /**
     * Return the next node
     * @return long
     */
    public long getNext() {
        int i = this.getCursor() + 1;
        if (i == this.getNodes().size()) {
            i = i - this.getNodes().size();
        }
        return this.getNode(i);
    }

    /**
     * Return the node number i of the ArrayList nodes
     * @param i
     * @return long
     */
    public long getNode(int i) {
        // System.out.println("i : "+i);
        return this.getNodes().get(i);
    }

    // getters&setters
    /**
     * Return the attribute nodes
     * @return ArrayList<Long>
     */
    public ArrayList<Long> getNodes() {
        return this.nodes;
    }

    /**
     * Return the previous node
     * @return long
     */
    public long getPrec() {
        int i = this.getCursor() - 1;
        if (i == -1) {
            i = i + this.getNodes().size();
        }
        return this.getNode(i);
    }

    /**
     * Replace the old value of the attribute cursor by the value of c
     * @param c
     */
    public void setCursor(int c) {
        this.cursor = c;
    }
}
